<script setup>
import {ref,onMounted} from 'vue'
import { Delete,Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import {artGetListService,artDeleteService} from '@/api/article.js'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
import { ElMessage } from 'element-plus'
const articleList = ref([])//文章列表
const total=ref(0)//总条数
const loading=ref(false)
const articleEditRef = ref()
const getArticlList= async()=>{
   loading.value=true
   const res = await artGetListService(params.value)
   articleList.value = res.data.data
   total.value = res.data.total
   loading.value=false
}
const onDelete = async(row)=>{
   await ElMessageBox.confirm('你确定要删除该文章吗','温馨提示',{
    type:'warning',
    confirmButtonText:'确认',
    cancelButtonText:'取消'
  })
  await artDeleteService(row.id)
  getArticlList()
  ElMessage.success('删除成功')
}
const params = ref({
   pagenum: 1,
   pagesize: 5,
   cate_id: '',//13818
   state: ''
})
getArticlList()
const handleCurrentChange=(e)=>{
   params.value.pagenum=e
   getArticlList()
}
const handleSizeChange=(e)=>{
   params.value.pagenum=1
   params.value.pagesize=e
   getArticlList()
}
// 按照最新的条件，重新检索，从第一页开始
const onSearch= () =>{
   params.value.pagenum =1
   getArticlList()
}
// 重置逻辑 => 将筛选条件清空，重新检索，从第一页开始展示
const onReset= () =>{
   params.value.cate_id=''
   params.value.state=''
   getArticlList()
}
const onAddArticle = () =>{
   articleEditRef.value.open({})
}
const onEidt = (row) =>{
  articleEditRef.value.open(row)
}
const onSuccess = (type) => {
   if(type === 'add'){
      //渲染最后一页，最好渲染最后一页
      const lastPage = Math.ceil((total.value+1)/params.value.pagesize)
      //更新成最大页码数
      params.value.pagenum = lastPage
   }
   getArticlList()
}
</script>
<template>
    <page-container title="文章管理">
       <template #extra>
         <el-button type="primary" @click="onAddArticle">添加文章</el-button>
       </template>
       <!-- 表单区域 -->
       <el-form >
         <el-form-item label="文章分类:">
            <!-- label是展示给用户看的，value是提交给后台的 -->
            <channel-select v-model="params.cate_id"></channel-select>
         </el-form-item>
         <el-form-item label="发布状态:" >
            <!-- 这里标记后台发布状态，就是通过中文标记的，已发布 -->
            <el-select v-model="params.state">
               <el-option label="已发布" value="已发布"></el-option>
               <el-option label="草稿" value="草稿"></el-option>
            </el-select>
         </el-form-item>
         <el-form-item>
         <el-button type="primary" @click="onSearch">搜索</el-button>
         <el-button @click="onReset">重置</el-button>
         </el-form-item>
       </el-form>
       <!-- 表格区域 -->
       <el-table :data="articleList" v-loading="loading">
         <el-table-column label="文章标题" prop="title">
            <template #default="{row}">
               <el-link type="primary" :underline="false">{{ row.title }}</el-link>
            </template>
         </el-table-column>
         <el-table-column label="分类" prop="cate_name"></el-table-column>
         <el-table-column label="发表时间" prop="pub_date">
            <template #default="{row}">
               {{ formatTime(row.pub_date) }}
            </template>
         </el-table-column>
         <el-table-column label="状态" prop="state"></el-table-column>
         <!-- 利用作用域插槽row可以获取当前的数据 -->
         <el-table-column label="操作">
            <template #default="{ row }">
               <el-button circle plain type="primary" :icon="Edit" @click="onEidt(row)"></el-button>
               <el-button circle plain type="danger" :icon="Delete" @click="onDelete(row)"></el-button>
            </template>
         </el-table-column>
       </el-table>
       <!-- 分页区域 -->
       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      :page-size="params.pagesize"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      style="justify-content: flex-end;">
    </el-pagination>
    <!-- 抽屉 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
    </page-container>
 </template>
 <style lang="scss" scoped>
 
 </style>